package com.phiture.erp.basic.web.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import com.phiture.erp.basic.core.pojo.dto.ErpMaterialCategoryListReqDTO;
import com.phiture.erp.basic.core.pojo.dto.ErpMaterialCategorySaveReqDTO;
import com.phiture.erp.basic.core.pojo.vo.ErpMaterialCategoryRespVO;
import com.phiture.erp.basic.core.service.ErpMaterialCategoryService;
import com.phiture.erp.basic.dal.entity.ErpMaterialCategoryDO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

/**
 * @author YH
 * @date 2025/3/31
 */

@Tag(name = "管理后台 - ERP 物料分类")
@RestController
@RequestMapping("/erp/material-category")
@RequiredArgsConstructor
public class ErpMaterialCategoryController {
    private final ErpMaterialCategoryService materialCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建物料分类")
    @PreAuthorize("@ss.hasPermission('erp:material-category:create')")
    public CommonResult<Long> createMaterialCategory(@Valid @RequestBody ErpMaterialCategorySaveReqDTO createReqDTO) {
        return success(materialCategoryService.createMaterialCategory(createReqDTO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料分类")
    @PreAuthorize("@ss.hasPermission('erp:material-category:update')")
    public CommonResult<Boolean> updateMaterialCategory(@Valid @RequestBody ErpMaterialCategorySaveReqDTO createReqDTO) {
        materialCategoryService.updateMaterialCategory(createReqDTO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:material-category:delete')")
    public CommonResult<Boolean> deleteMaterialCategory(@RequestParam("id") Long id) {
        materialCategoryService.deleteMaterialCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:material-category:query')")
    public CommonResult<ErpMaterialCategoryRespVO> getMaterialCategory(@RequestParam("id") Long id) {
        ErpMaterialCategoryDO category = materialCategoryService.getMaterialCategory(id);
        return success(BeanUtils.toBean(category, ErpMaterialCategoryRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得物料分类列表")
    @PreAuthorize("@ss.hasPermission('erp:material-category:query')")
    public CommonResult<List<ErpMaterialCategoryRespVO>> getMaterialCategoryList(@Valid ErpMaterialCategoryListReqDTO listReqDTO) {
        List<ErpMaterialCategoryDO> list = materialCategoryService.getMaterialCategoryList(listReqDTO);
        return success(BeanUtils.toBean(list, ErpMaterialCategoryRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得物料分类精简列表", description = "只包含被开启的分类，主要用于前端的下拉选项")
    public CommonResult<List<ErpMaterialCategoryRespVO>> getMaterialCategorySimpleList() {
        List<ErpMaterialCategoryDO> list = materialCategoryService.getMaterialCategoryList(
                new ErpMaterialCategoryListReqDTO().setStatus(CommonStatusEnum.ENABLE.getStatus()));
        return success(convertList(list, category -> new ErpMaterialCategoryRespVO()
                .setId(category.getId()).setName(category.getName()).setParentId(category.getParentId())));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料分类 Excel")
    @PreAuthorize("@ss.hasPermission('erp:material-category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialCategoryExcel(@Valid ErpMaterialCategoryListReqDTO listReqDTO, HttpServletResponse response) throws IOException {
        List<ErpMaterialCategoryDO> list = materialCategoryService.getMaterialCategoryList(listReqDTO);
        // 导出 Excel
        ExcelUtils.write(response, "物料分类.xls", "数据", ErpMaterialCategoryRespVO.class,
                BeanUtils.toBean(list, ErpMaterialCategoryRespVO.class));
    }
}
